package exer;

import java.util.Stack;

/**
 * @author tongchen
 * @create 2022-10-18 9:02
 */
public class MinStack {
    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(512);
        minStack.push(-1024);
        minStack.push(-1024);
        minStack.push(512);
        minStack.pop();
        minStack.getMin();
        minStack.pop();
        minStack.getMin();
        minStack.pop();
        System.out.println(minStack.getMin());
    }
    Stack<Integer> stack;
    Stack<Integer>miniStack;

    public MinStack() {
        //创建两个栈空间
        stack=new Stack<>();
        miniStack=new Stack<>();

    }

    public void push(int val) {
        //优先处理栈为空的情况
        if(stack.isEmpty()){
            stack.push(val);
            miniStack.push(val);
        }
        else{
            stack.push(val);
            if(val<=miniStack.peek()){
                miniStack.push(val);
            }
        }

    }

    public void pop() {
        //拿出时仍然要对miniStack进行判断
        if(stack.peek().intValue()==miniStack.peek().intValue()){
            miniStack.pop();
        }
        stack.pop();
    }

    public int top() {
        return  stack.peek();

    }

    public int getMin() {
        return   miniStack.peek();
    }

}
